<?php
namespace Admin\Controller;
class NodeController extends CommonController {
 
	public function _filter(&$map)
	{
    if (!empty($_GET['group_id'])) {
            $map['group_id'] =  $_GET['group_id'];
            $this->assign('nodeName','分组');
        }elseif((empty($_POST['name'])&&empty($_POST['title'])) && !isset($map['pid']) ) {
			$map['pid']	=	0;
		}
		if($_GET['pid']!=''){
			$map['pid']=$_GET['pid'];
		}
		if($_REQUEST['name']){
			$map['name']=array('like','%'.trim($_REQUEST['name']).'%');
		}
		if($_REQUEST['title']){
			$map['title']=array('like','%'.trim($_REQUEST['title']).'%');
		}
		$_SESSION['currentNodeId']	=	$map['pid'];
		//获取上级节点
		$node  = M("Node");
        if(isset($map['pid'])) {
            if($node->getById($map['pid'])) {
                $this->assign('level',$node->level+1);
                $this->assign('nodeName',$node->name);
            }else {
                $this->assign('level',1);
            }
        }
	}


  
    function index() {
        //列表过滤器，生成查询Map对象
        $map = array();
        if (method_exists ( $this, '_filter' )) {
            $this->_filter ( $map );
        }

        $model = D (CONTROLLER_NAME);
        if (! empty ( $model )) {
            //$this->_list ( $model, $map );
            $this->assign ( 'lists', $model->where($map)->select() );
        }

        $this->display ();
        return;
    }

	public function _before_index() {
		$model	=	M("Group");
		$list	=	$model->where('status=1')->getField('id,title');
		$this->assign('groupList',$list);
	}

	//获取配置类型
	public function ajax_get_add_data() {
		$model	=	M("Group");
		$list	=	$model->where('status=1')->select();
		$node	=	M("Node");
		$node->getById($_SESSION['currentNodeId']);
        echo json_encode(array('list'=>$list,'pid'=>$node->id,'level'=>($node->level+1)));
	}

	 public function _before_edit() {
	 	$model	=	M("Group");
	 	$list	=	$model->where('status=1')->select();
	 	$this->assign('list',$list);
	 }

}
?>